package object.obj3D.enemy;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cube {
	public FloatBuffer _vertexBuffer;
	public FloatBuffer _colorBuffer;
	public ShortBuffer _indexBuffer;
	public FloatBuffer normBuffer;
	
	
	public void init(){
		
		float vertices[] = {
				-86.325f,	-67.346f,	85.713f,
				85.713f,	-67.346f,	85.713f,
				-86.325f,	50.816f,	85.713f,
				85.713f,	50.816f,	85.713f,
				
				85.713f,	-67.346f,	85.713f,
				85.713f,	-67.346f,	0,
				85.713f,	50.816f,	85.713f,
				85.713f,	50.816f,	0,
				
				85.713f,	-67.346f,	0,
				-86.325f,	-67.346f,	0,
				85.713f,	50.816f,	0,
				-86.325f,	50.816f,	0,
				
				-86.325f,	-67.346f,	0,
				-86.325f,	-67.346f,	85.713f,
				-86.325f,	50.816f,	0,
				-86.325f,	50.816f,	85.713f,
	    		
	    		-86.325f,	-67.346f,	0,
	    		85.713f,	-67.346f,	0,
	    		-86.325f,	-67.346f,	85.713f,
	    		85.713f,	-67.346f,	85.713f,
	    		
	    		-86.325f,	50.816f,	85.713f,
	    		85.713f,	50.816f,	85.713f,
	    		-86.325f,	50.816f,	0,
	    		85.713f,	50.816f,	0
	    		
			};
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
		vbb.order(ByteOrder.nativeOrder());
		_vertexBuffer = vbb.asFloatBuffer();
		
		_vertexBuffer.put(vertices);
		_vertexBuffer.position(0);
		
		float norms[] = {
				0.0f, 0.0f, 1.0f,
				0.0f, 0.0f, 1.0f,
				0.0f, 0.0f, 1.0f,
				0.0f, 0.0f, 1.0f,
				
				1.0f, 0.0f, 0.0f,
				1.0f, 0.0f, 0.0f,
				1.0f, 0.0f, 0.0f,
				1.0f, 0.0f, 0.0f,
				
				0.0f, 0.0f, -1.0f,
				0.0f, 0.0f, -1.0f,
				0.0f, 0.0f, -1.0f,
				0.0f, 0.0f, -1.0f,
				
				-1.0f, 0.0f, 0.0f,
				-1.0f, 0.0f, 0.0f,
				-1.0f, 0.0f, 0.0f,
				-1.0f, 0.0f, 0.0f,
				
				0.0f, -1.0f, 0.0f,
				0.0f, -1.0f, 0.0f,
				0.0f, -1.0f, 0.0f,
				0.0f, -1.0f, 0.0f,
				
				0.0f, 1.0f, 0.0f,
				0.0f, 1.0f, 0.0f,
				0.0f, 1.0f, 0.0f,
				0.0f, 1.0f, 0.0f,
		};
		
		ByteBuffer nbb = ByteBuffer.allocateDirect(norms.length*4);
		nbb.order(ByteOrder.nativeOrder());
		normBuffer = nbb.asFloatBuffer();
		
		normBuffer.put(norms);
		normBuffer.position(0);
				
		float colors[] = {
				1f,	0f,	0f,	1f,
				1f,	0f,	0f,	1f,
				1f,	0f,	0f,	1f,
				1f,	0f,	0f,	1f,
				
				0f,	1f,	0f,	1f,
				0f,	1f, 0f,	1f,
				0f,	1f,	0f,	1f,
				0f,	1f,	0f,	1f,
				
				0f,	0f,	1f,	1f,
				0f,	0f, 1f,	1f,
				0f,	0f,	1f,	1f,
				0f,	0f,	1f,	1f,
				
				0f,	1f,	1f,	1f,
				0f,	1f, 1f,	1f,
				0f,	1f,	1f,	1f,
				0f,	1f,	1f,	1f,
				
				1f,	0f,	1f,	1f,
				1f,	0f, 1f,	1f,
				1f,	0f,	1f,	1f,
				1f,	0f,	1f,	1f,
				
				1f,	1f,	0f,	1f,
				1f,	1f, 0f,	1f,
				1f,	1f,	0f,	1f,
				1f,	1f,	0f,	1f
		};
		ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
		cbb.order(ByteOrder.nativeOrder());
		_colorBuffer = cbb.asFloatBuffer();
		
		_colorBuffer.put(colors);
		_colorBuffer.position(0);
		
		
		short indices[] = {
				//Faces definition
	    		0, 1, 3,	0, 3, 2, 		//Face front
	    		4, 5, 7,	4, 7, 6, 		//Face right
	    		8, 9, 11,	8, 11, 10, 		//... 
	    		12, 13, 15,	12, 15, 14, 	
	    		16, 17, 19,	16, 19, 18, 	
	    		20, 21, 23,	20, 23, 22
		};
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
		ibb.order(ByteOrder.nativeOrder());
		_indexBuffer = ibb.asShortBuffer();
		
		
		_indexBuffer.put(indices);
		_indexBuffer.position(0);
		
	}
	
	public Cube(){
		init();
	}
	
	public void draw(GL10 gl){
		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
		gl.glNormalPointer(GL10.GL_FLOAT, 0, normBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
	}
}
